//
//  StarTableViewCell.m
//  Mango
//
//  Created by lanou3g on 16/5/11.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "StarTableViewCell.h"
#import "StarCollectionViewCell.h"
@interface StarTableViewCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation StarTableViewCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"star";
    StarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[StarTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:ID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(100, 150);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//横向滚动
        layout.minimumLineSpacing = 45;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        self.collectionView.dataSource = self;
        self.collectionView.delegate = self;
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.dataSource = [NSMutableArray array];
        [self.collectionView registerNib:[UINib nibWithNibName:@"StarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cStar"];
        [self.contentView addSubview:self.collectionView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
        [self updateSkinModel];
    }
    return self;
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {
        self.collectionView.backgroundColor = [UIColor colorWithRed:34/255.0 green:30/255.0 blue:33/255.0 alpha:1.0];
    }
    [self.collectionView reloadData];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cStar" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
#pragma mark --private Method--更新皮肤模式
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//日间模式
        [cell updateToNightSkinMode];
    }else {
        [cell updateToDaySkinMode];
    }
    return cell;
}

- (void)setDataSource:(NSMutableArray *)dataSource {
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(didClickStarItemAtIndexPath:)]) {
        [self.delegate didClickStarItemAtIndexPath:indexPath];
    }
}

#pragma mark 切换至日间模式
-(void)updateToDaySkinMode {
    self.collectionView.backgroundColor = [UIColor whiteColor];
}
-(void)updateToNightSkinMode {
    self.collectionView.backgroundColor = [UIColor colorWithRed:42/255.0 green:39/255.0 blue:43/255.0 alpha:1.0];
}


@end
